package com.jxf.demo.service;

import cn.hutool.core.collection.CollectionUtil;
import com.jxf.demo.dto.*;
import org.kabeja.dxf.DXFLayer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class AbstractDXFParseService {

    public List<DXFPointInfo> parse(DXFLayer dxfLayer) {
        List lineEntities = dxfLayer.getDXFEntities(getType());
        if (CollectionUtil.isEmpty(lineEntities)) {
            return new ArrayList<>();
        }
        // 结果解析
        List<DXFPointInfo> result = doParse(lineEntities);
        // 获取外接矩形

        return result;
    }

    public Rectangle getRectangle(List<BigDecimal> pointXList, List<BigDecimal> pointYList) {
        BigDecimal minX = Collections.min(pointXList);
        BigDecimal maxX = Collections.max(pointXList);
        BigDecimal minY = Collections.min(pointYList);
        BigDecimal maxY = Collections.max(pointYList);
        System.out.println("(" + minX + "," + minY + "),(" + maxX + "," + maxY + ")");
        // 外接矩形
        BigDecimal width = maxX.subtract(minX);
        BigDecimal height = maxY.subtract(minY);
        return Rectangle.builder().startingPointCoordinates(new BigDecimal[]{minX, minY})
                .widthHeight(new BigDecimal[]{width, height})
                .realWidthHeight(new BigDecimal[]{width, height})
                .build();
    }


    /**
     * 具体解析逻辑
     *
     * @param lineEntities
     * @return
     */
    public abstract List<DXFPointInfo> doParse(List lineEntities);

    /**
     * 图形类型
     *
     * @return
     */
    public abstract String getType();
}
